package com.synergy.stockflow.team.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.team.entity.SchedulingPlan;
import com.synergy.stockflow.team.service.SchedulingPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 排班计划控制器
 * 处理排班计划的查询、新增、修改、删除等操作
 */
@RestController
@RequestMapping("/scheduling-plan")
public class SchedulingPlanController {
    @Autowired
    private SchedulingPlanService schedulingPlanService;


    /**
     * 分页查询排班计划列表
     */
    @GetMapping("/list")
    public Result<Page<SchedulingPlan>> list(
            String teamType,
            String planId,
            String planName,
            String startDate,
            String endDate,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size
    ) {
        QueryWrapper<SchedulingPlan> queryWrapper = new QueryWrapper<>();

        if (teamType != null && !teamType.isEmpty()) {
            queryWrapper.eq("team_type", teamType);
        }
        if (planId != null && !planId.isEmpty()) {
            queryWrapper.like("plan_id", planId);
        }
        if (planName != null && !planName.isEmpty()) {
            queryWrapper.like("plan_name", planName);
        }
        if (startDate != null && !startDate.isEmpty()) {
            queryWrapper.ge("start_date", startDate);
        }
        if (endDate != null && !endDate.isEmpty()) {
            queryWrapper.le("end_date", endDate);
        }

        queryWrapper.orderByDesc("create_time");

        Page<SchedulingPlan> page = new Page<>(current, size);
        schedulingPlanService.page(page, queryWrapper);
        return Result.success(page);
    }

    /**
     * 新增排班计划
     */
    @PostMapping("/add")
    public Result<?> add(@RequestBody SchedulingPlan schedulingPlan) {
        try {
            boolean saved = schedulingPlanService.save(schedulingPlan);
            return saved ? Result.success("新增成功") : Result.error(500, "新增失败");
        } catch (Exception e) {
            return Result.error(500, "新增失败：" + e.getMessage());
        }
    }

    /**
     * 修改排班计划
     */
    @PutMapping("/update")
    public Result<?> update(@RequestBody SchedulingPlan schedulingPlan) {
        try {
            boolean updated = schedulingPlanService.updateById(schedulingPlan);
            return updated ? Result.success("修改成功") : Result.error(500, "修改失败");
        } catch (Exception e) {
            return Result.error(500, "修改失败：" + e.getMessage());
        }
    }

    /**
     * 批量删除排班计划
     */
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestBody List<String> planIds) {
        try {
            boolean removed = schedulingPlanService.removeByIds(planIds);
            return removed ? Result.success("删除成功") : Result.error(500, "删除失败");
        } catch (Exception e) {
            return Result.error(500, "删除失败：" + e.getMessage());
        }
    }
}
